//
//  HomePageHeadView.m
//  BakeCenter
//
//  Created by lanou3g on 15/9/19.
//  Copyright (c) 2015年 Baked. All rights reserved.
//

#import "HomePageHeadView.h"
#import "TouchImageView.h"

@implementation HomePageHeadView

- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSMutableArray *)imageArray titleArray:(NSMutableArray *)titleArray delegate:(id)delegate action:(SEL)action timer:(NSTimeInterval)timer selector:(SEL)selector
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor yellowColor];
        
        NSMutableArray *insertArray = [NSMutableArray arrayWithArray:imageArray];
        [insertArray insertObject:[imageArray lastObject] atIndex:0];
        [insertArray insertObject:[imageArray objectAtIndex:0] atIndex:imageArray.count + 1];
        
        NSMutableArray *ChangedArray = [NSMutableArray arrayWithArray:titleArray];
        [ChangedArray insertObject:[titleArray lastObject] atIndex:0];
        [ChangedArray insertObject:[titleArray objectAtIndex:0] atIndex:imageArray.count + 1];
        
        
        //设置 scrollView 的 contentSize
        self.contentSize = CGSizeMake(kScreenWidth * insertArray.count, 0);
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = YES;
        self.bounces = NO;
        self.delegate = delegate;
        self.contentOffset = CGPointMake(kScreenWidth, 0);
        
        //循环创建imageView
        
        for (int i = 0; i < insertArray.count; i ++) {
            TouchImageView *touchView = [[TouchImageView alloc] initWithFrame:CGRectMake(i * kScreenWidth, 0, kScreenWidth,self.bounds.size.height) target:delegate action:action];
            [touchView sd_setImageWithURL:[NSURL URLWithString:insertArray[i]] placeholderImage:[UIImage imageNamed:@"111.jpg"]];
            touchView.backgroundColor = [UIColor redColor];
            [self addBlurViewWithTitle:ChangedArray[i] inTouchView:touchView];
            
            [self addSubview:touchView];
            
        }
        
        [NSTimer scheduledTimerWithTimeInterval:timer target:delegate selector:selector userInfo:nil repeats:YES];
        
    }
    return self;
}


-(void)addBlurViewWithTitle:(NSString *)title inTouchView:(TouchImageView *)touchView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 140, kScreenWidth, 30)];
    [touchView addSubview:view];
    //创建模糊视图
    UIVisualEffectView *backVisual = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)]];
    //将模糊视图大小等同与自身
    backVisual.frame = view.bounds;
    
    //设置模糊视图透明度
    backVisual.alpha = 0.8;
    [view addSubview:backVisual];
    
    UILabel *titleLable = [[UILabel alloc] initWithFrame:backVisual.bounds];
    titleLable.backgroundColor = [UIColor clearColor];
    titleLable.text = title;
    titleLable.font = [UIFont boldSystemFontOfSize:15];
    titleLable.textColor = [UIColor whiteColor];
    [backVisual addSubview:titleLable];
    
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
